package com.mall.controller;

import com.mall.domain.Page;
import com.mall.domain.Role;
import com.mall.domain.User;
import com.mall.service.impl.UserServiceImpl;
import com.mall.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: liangY
 * @date: 2020-12-25 11:51
 * @description: 用户web层
 */
@Controller
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserServiceImpl userService;


    /**
     * 用户登录
     * @param loginUser
     * @return
     */
    @RequestMapping("/login")
    public  @ResponseBody
    Map<String,Object> login(@RequestBody User loginUser) {
        Map<String,Object> loginMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        String token = TokenUtil.sign(loginUser.getUsername());
        User user = userService.login(loginUser);
        dataMap.put("token",token);
        dataMap.put("user",user);
        loginMap.put("data",dataMap);
        Map<String,Object> metaMap = new HashMap<>();
        if(user != null){
            metaMap.put("msg","登录成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","登录失败");
            metaMap.put("status",0);
        }
        loginMap.put("meta",metaMap);
        return  loginMap;
    }

    /**
     * 查询所有用户
     * @param page
     * @return
     */

    @RequestMapping("/findAll")
    public @ResponseBody
    Map<String, Object> findAll(@RequestBody Page page){
        Map<String,Object> userMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //获取用户集合
        List<User> users = userService.findAll(page);
        //放入dataMap集合
        dataMap.put("users",users);
        dataMap.put("total",userService.findTotal());
        //放入Map集合
        userMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        metaMap.put("msg","获取用户列表成功");
        metaMap.put("status",200);

        userMap.put("meta",metaMap);
        return userMap;
    }

    /**
     * 更新指定用户
     * @param user
     * @return
     */
    @RequestMapping("/update")
    public @ResponseBody
    Map<String, Object> update(@RequestBody User user){
        Map<String,Object> userMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("User",user);
        //放入Map集合
        userMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = userService.update(user);
        if(flag > 0){
            metaMap.put("msg","更新用户成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新用户失败");
            metaMap.put("status",0);
        }
        userMap.put("meta",metaMap);
        return userMap;
    }

    @RequestMapping("/update2")
    public @ResponseBody
    Map<String, Object> update2(@RequestBody User user){
        Map<String,Object> userMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("User",user);
        //放入Map集合
        userMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = userService.update2(user);
        if(flag > 0){
            metaMap.put("msg","更新密码成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新用户失败");
            metaMap.put("status",0);
        }
        userMap.put("meta",metaMap);
        return userMap;
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping("/add")
    public @ResponseBody
    Map<String, Object> add(@RequestBody User user){
        Map<String,Object> userMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入dataMap集合
        dataMap.put("User",user);
        //放入Map集合
        userMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = userService.add(user);
        if(flag > 0){
            metaMap.put("msg","添加用户成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","添加用户失败");
            metaMap.put("status",0);
        }
        userMap.put("meta",metaMap);
        return userMap;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public @ResponseBody
    Map<String, Object> add(Integer id){
        Map<String,Object> userMap = new HashMap<>();
        Map<String,Object> dataMap = new HashMap<>();
        //放入Map集合
        userMap.put("data",dataMap);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = userService.del(id);
        if(flag > 0){
            metaMap.put("msg","删除用户成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","删除用户失败");
            metaMap.put("status",0);
        }
        userMap.put("meta",metaMap);
        return userMap;
    }

    /**
     * 更新用户角色
     * @param request
     * @return
     */
    @RequestMapping("/updateRole")
    public @ResponseBody
    Map<String, Object> updateRole(HttpServletRequest request){
        Integer user_id = Integer.parseInt(request.getParameter("user_id"));
        Integer role_id = Integer.parseInt(request.getParameter("role_id"));
        Map<String,Object> userMap = new HashMap<>();
        //放入Map集合
        userMap.put("data",null);
        //创建状态Map
        Map<String,Object> metaMap = new HashMap<>();
        int flag = userService.updateRole(user_id,role_id);
        if(flag > 0){
            metaMap.put("msg","更新用户角色成功");
            metaMap.put("status",200);
        } else {
            metaMap.put("msg","更新用户角色失败");
            metaMap.put("status",0);
        }
        userMap.put("meta",metaMap);
        return userMap;
    }


}
